package com.itheima.webaitliasserver.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.webaitliasserver.mapper.EmpExprMapper;
import com.itheima.webaitliasserver.mapper.EmpMapper;
import com.itheima.webaitliasserver.pojo.*;
import com.itheima.webaitliasserver.service.EmpLogService;
import com.itheima.webaitliasserver.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName: xiaoZhang
 * @Description:
 * @Author: Zhang
 * @CreateDate: 2025-07-23 17:28
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult<Emp> queryEmpList(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        Long total = empMapper.queryEmpCount();
        List<Emp> empList = empMapper.queryEmpEveryPageRecord(start , pageSize);
        return new PageResult<Emp>();
    }

    @Override
    public PageResult<Emp> queryEmpListUsePageHelperService(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        System.out.println(PageHelper.getLocalPage() + "------");
        List<Emp> empList = empMapper.queryPageHelperEmpRecord();

        PageInfo<Emp> pageInfo  = new PageInfo<>(empList);

        return new PageResult<Emp>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public PageResult<Emp> queryEmpByOptions(EmpEntryParam entryParam) {
        PageInfo<Emp> pageInfo  = null;
        try {
            PageHelper.startPage(entryParam.getPage(), entryParam.getPageSize()); // 分页处理

            List<Emp> empList = empMapper.queryEmpByOptionMapper(entryParam); // 查询数据库中符合条件数据

            pageInfo = new PageInfo<>(empList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional(rollbackFor = { Exception.class })
    public void addEmpRecord(Emp emp) {
        try {
            // 设置默认信息
            emp.setUpdateTime(LocalDateTime.now());
            emp.setCreateTime(LocalDateTime.now());

            // 插入员工信息
            log.info("emp: {}" , emp);
            empMapper.insertEmpRecordMapper(emp);

            List<EmpExpr> empExprList = emp.getExprList();

            boolean empExprExist =  CollectionUtils.isEmpty(empExprList);
            // 如果员工经历不为空  则将每一个员工id赋值为员工工作经历id
            if(!empExprExist){
                empExprList.forEach(empExpr -> {
                    empExpr.setEmpId(emp.getId()); // 设置员工经历每条数据的 员工id为当前用户id
                });
            }
            // 插入员工 经历数据
            empExprMapper.batchInsertEmpExprList(empExprList);
        }finally {
            // 验证 Transactional 事务的传播行为 可以为 不创建新事务 和创建一个新的事务 与原事务断开关联
            EmpLog empLog = new EmpLog();
            empLog.setId(null);
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo(emp.toString());
            empLogService.insertEmpLog(empLog);
        }

    }


    // 根据ID删除员工记录
    @Override
    @Transactional(rollbackFor = { Exception.class }) // 回滚事务
    public void removeEmpRecordById(Integer[] ids) {
        log.info("removeEmpRecordById" , Arrays.toString(ids));
        if(ids.length == 0){
            return;
        }
        //删除员工记录表
        empMapper.batchRemoveEmpRecordMapper(ids);
        //删除 员工经历表记录数据
        empMapper.removeEmpExprsMapper(ids);
    
    }

    @Override
    public Emp queryEmpRecordById(Integer id) {
        // 优化 换成一个接口实现
//        Emp emp = empMapper.queryEmpRecordById(id);
//        if(emp.getId() != null){
//            List<EmpExpr> exprList = empMapper.queryEmpRecordByEmpId(id);
//            if(exprList.size() != 0){
//                emp.setExprList(exprList);
//            }else{
//                emp.setExprList(new ArrayList<>());
//            }
//        }
//        return emp;
//    }
        // 一个sql查询实现
        Emp emp = empMapper.queryEmpRecordById(id);
        return  emp;
    }

    @Transactional(rollbackFor = { Exception.class })
    @Override
    public Emp updateEmpRecord(Emp emp) {
        log.info("empId:{}" , emp.getId());
        // 设置员工记录更新时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmpInfoById(emp); // 更新员工信息
        // 删除员工经历信息
        empMapper.batchRemoveEmpExprsRecordMapper(new Integer[]{emp.getId()});
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){ // 如果员工经历不为空则 更新员工经历
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            // 设置之每一个员工记录id为 当前员工信息id
            empExprMapper.batchInsertEmpExprList(emp.getExprList()); // 插入数据
        }
        new Error("自定义错误");
        return null;
    }
}